package com.thoughtworks.jspecsupport.settings;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class JSpecSupportSettingsManager implements SearchableConfigurable {

    private JSpecSupportSettingsPanel panel;
    private JSpecSupportSettings settings;

    public static JSpecSupportSettingsManager getInstance(Project project) {
        return ShowSettingsUtil.getInstance().findProjectConfigurable(project, JSpecSupportSettingsManager.class);
    }

    public JSpecSupportSettingsManager(Project project) {
        settings = JSpecSupportSettings.getInstance(project);
    }

    public String getId() {
        return "JSpecSupportId";
    }

    public Runnable enableSearch(String option) {
        return null;
    }

    @Nls
    public String getDisplayName() {
        return "JSpec Unit Test";
    }

    @Nullable
    public Icon getIcon() {
        return null;
    }

    @Nullable
    public String getHelpTopic() {
        return "JSpec support help topic";
    }

    public JComponent createComponent() {
        if (panel == null) {
            panel = new JSpecSupportSettingsPanel(settings.getProdJsFolder(),
                                                  settings.getTestLibFolder());
        }
        return panel.getPanel();
    }

    public boolean isModified() {
        return true;
    }

    public void apply() throws ConfigurationException {
        System.out.println("apply...");
        settings.setProdJsFolder(panel.getProdJsFolder());
        settings.setTestLibFolder(panel.getTestLibFolder());
    }

    public void reset() {
        settings.setProdJsFolder("");
        settings.setTestLibFolder("");
    }

    public void disposeUIResources() {
        panel = null;
        System.out.println("dispose ui");
    }

    public void modified() {
    }
}
